package ps.client.gui.dpsoverlay;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import ps.client.gui.MainFrame;
import ps.client.gui.util.PsOverlayWindow;
import ps.net.DpsParseContent;


@SuppressWarnings("serial")
public class DpsWindow extends PsOverlayWindow {
    
    public static Font FONT_PARSE_TIME = new Font("Arial", Font.BOLD, 12);
    public static Font FONT_PARSE_DPS = new Font("Arial", Font.BOLD, 16);
    
    MainFrame mainFrame;
    
    JPanel innerContentPanel = new JPanel(new BorderLayout(0, 3));
    
    JPanel parseTitlePanel = new JPanel(new BorderLayout(0, 3));
    JLabel parseTimeLabel = new JLabel();
    JLabel parseDpsLabel = new JLabel();
    DpsListModel dpsListModel = new DpsListModel();
    JList dpsList = new JList(dpsListModel);
    
    public DpsWindow(MainFrame mainFrame, String prefNode) {
        super(null, TOP, prefNode);
        this.mainFrame = mainFrame;
        
        titleLabel.setText("DPS");
        setMinimumSize(new Dimension(128, 20));
        
        contentPanel.add(innerContentPanel, BorderLayout.CENTER);
        
        innerContentPanel.setVisible(false);
        innerContentPanel.setBorder(new EmptyBorder(2, 2, 1, 2));
        innerContentPanel.setBackground(Color.BLACK);
        innerContentPanel.add(parseTitlePanel, BorderLayout.NORTH);
        innerContentPanel.add(dpsList, BorderLayout.CENTER);
        
        parseTitlePanel.setBackground(Color.BLACK);
        parseTitlePanel.add(parseTimeLabel, BorderLayout.NORTH);
        parseTitlePanel.add(parseDpsLabel, BorderLayout.CENTER);
       
        parseTimeLabel.setHorizontalAlignment(JLabel.CENTER);
        parseTimeLabel.setFont(FONT_PARSE_TIME);
        parseTimeLabel.setForeground(Color.WHITE);
        
        parseDpsLabel.setHorizontalAlignment(JLabel.CENTER);
        parseDpsLabel.setFont(FONT_PARSE_DPS);
        parseDpsLabel.setForeground(Color.YELLOW);
        
        dpsList.setBackground(Color.BLACK);
        dpsList.setCellRenderer(new DpsListCellRenderer());
        
        buttonPanel.remove(configButton);
        buttonPanel.remove(biggerButton);
        buttonPanel.remove(smallerButton);
        
        pack();
        
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                innerContentPanel.setVisible(false);
                dpsListModel.clear();
                pack();
            }
        });
    }
    
    @Override
    protected void loadSettings() {
        setLocation(20, 240);
        super.loadSettings();
    }
    
    public void setDpsParseContent(DpsParseContent content) {
        innerContentPanel.setVisible(false);
        
        String timeStr = content.getTime();
        try {
            int dmg = Integer.parseInt(content.getDamage());
            timeStr += "   " + (dmg / 1000000) + "." + (dmg % 1000000) / 100000 + "M";
        } catch (Exception ex) {
        }
        parseTimeLabel.setText(timeStr);
        
        String dpsStr = "0.0K";
        try {
            int dps = Integer.parseInt(content.getDps());
            dpsStr = (dps / 1000) + "." + (dps % 1000) / 100 + "K";
        } catch (Exception ex) {
        }
        parseDpsLabel.setText(dpsStr);
        
        dpsListModel.setData(content);

        innerContentPanel.setVisible(true);
        pack();
    }

}
